package com.ruoyi.applySender.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.applySender.domain.Applysender;
import com.ruoyi.applySender.service.IApplysenderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 身份审核Controller
 * 
 * @author ruoyi
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/applySender/applySender")
public class ApplysenderController extends BaseController
{
    @Autowired
    private IApplysenderService applysenderService;


    //通过身份申请
    @PreAuthorize("@ss.hasPermi('applySender:applySender:edit')")
    @PutMapping("/pass/{id}/{uid}")
    public AjaxResult ApplyPass(@PathVariable("id") int id,@PathVariable("uid") int uid){

        //首先修改申请记录为已通过
        int i = applysenderService.passApply(id);
        if (i > 0) {
            //将用户身份修改
            int i1 = applysenderService.changeRole(uid);
            return AjaxResult.success(i1>0);
        }
        return AjaxResult.success(false);
    }



    //驳回身份申请
    @PreAuthorize("@ss.hasPermi('applySender:applySender:edit')")
    @PutMapping("/back/{id}")
    public AjaxResult ApplyBack(@PathVariable int id){
        int i = applysenderService.backApply(id);
        return AjaxResult.success(i>0);
    }

    /**
     * 查询身份审核列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(Applysender applysender)
    {
        startPage();
        List<Applysender> list = applysenderService.selectApplysenderList(applysender);
        return getDataTable(list);
    }

    /**
     * 导出身份审核列表
     */
    @PreAuthorize("@ss.hasPermi('applySender:applySender:export')")
    @Log(title = "身份审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Applysender applysender)
    {
        List<Applysender> list = applysenderService.selectApplysenderList(applysender);
        ExcelUtil<Applysender> util = new ExcelUtil<Applysender>(Applysender.class);
        util.exportExcel(response, list, "身份审核数据");
    }

    /**
     * 获取身份审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('applySender:applySender:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(applysenderService.selectApplysenderById(id));
    }

    /**
     * 新增身份审核
     */
    @Anonymous
    @Log(title = "身份审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Applysender applysender)
    {
        return toAjax(applysenderService.insertApplysender(applysender));
    }

    /**
     * 修改身份审核
     */
    @PreAuthorize("@ss.hasPermi('applySender:applySender:edit')")
    @Log(title = "身份审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Applysender applysender)
    {
        return toAjax(applysenderService.updateApplysender(applysender));
    }

    /**
     * 删除身份审核
     */
    @PreAuthorize("@ss.hasPermi('applySender:applySender:remove')")
    @Log(title = "身份审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(applysenderService.deleteApplysenderByIds(ids));
    }
}
